package com.example.superyi.myteam.abilitybox.createteams;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.superyi.myteam.R;
import com.example.wjy.MyTeamActivity;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttp;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;

public class CreateTeamActivity extends AppCompatActivity implements View.OnClickListener {

    Toolbar toolbar;
    Spinner spinner;
    Button button;
    EditText name, numOfAll, introduction;
    String kind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_team);

        button = findViewById(R.id.create);
        name = findViewById(R.id.ct_TName);
        numOfAll = findViewById(R.id.editTextNumber);
        introduction = findViewById(R.id.editTextTextPersonName);
        spinner = findViewById(R.id.spinner2);
        toolbar = findViewById(R.id.toolbar5);
        setSupportActionBar(toolbar);

        button.setOnClickListener(this);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //获取item的内容
                kind = (String) spinner.getItemAtPosition(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.create:

                Intent intent = new Intent(CreateTeamActivity.this, MyTeamActivity.class);
                if (!"".equals(name.getText().toString()))
                    intent.putExtra("name", name.getText().toString());
                else {
                    Toast.makeText(this, "小队名不能为空！", Toast.LENGTH_SHORT).show();
                    break;
                }
                intent.putExtra("kind", kind);
                if ("".equals(numOfAll.getText().toString()) || Integer.parseInt(numOfAll.getText().toString()) <= 1) {
                    Toast.makeText(this, "你的小队人数不合法！", Toast.LENGTH_SHORT).show();
                    break;
                }
                intent.putExtra("numOfAll", Integer.valueOf(numOfAll.getText().toString()));
                intent.putExtra("introduction", introduction.getText().toString());
                //传入一个标志告诉下一个activity是否应该插入新的小队
                intent.putExtra("tag", "0");
                startActivity(intent);
                this.finish();
                break;
            default:
                break;
        }
    }
}
